<template>
  <div>
    <header>
      <nav-header></nav-header>
    </header>
    <div class="main">
      <div class="cart-filter-bar clearfix">
        <div class="switch-cart">
          全部商品<svg style="width: 20px; height: 20px"
               class="icon icon-cart">
            <use xlink:href="#icon-cart"></use>
          </svg>{{ cartNumber }}
        </div>
        <div class="cart-store">
          <svg style="width: 20px; height: 20px"
               class="icon icon-address">
            <use xlink:href="#icon-address"></use>
          </svg>
          <span style="margin-right: 10px">配送至:</span>
          <el-cascader size="small"
                       :options="options"
                       v-model="region"
                       @change="handleRegionChange">
          </el-cascader>
        </div>
      </div>

      <!-- 购物车表格 -->
      <div class="cart-body">
        <el-table :row-class-name="tableRowClassName"
                  :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
                  ref="cartTable"
                  :data="cartTableData"
                  style="width: 100%"
                  border
                  stripe
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"
                           width="55"></el-table-column>
          <el-table-column label="商品"
                           width="300">
            <template slot-scope="scope">
              <el-image style="width: 150px; height: 150px"
                        :src="scope.row.src"></el-image><span>{{ scope.row.medName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单价"
                           width="100"
                           align="center"><template slot-scope="scope">¥ {{ scope.row.medPrice }}</template></el-table-column>
          <el-table-column label="数量"
                           align="center"
                           width="250">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.ordCount"
                               @change="changeNum"
                               :min="1"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="小计"
                           width="150"
                           align="center"><template slot-scope="scope">
              ¥ {{ totalPrice(scope.row.medPrice, scope.row.ordCount) }}</template></el-table-column>
          <el-table-column label="操作"
                           align="center"><template slot-scope="scope">
              <el-button size="small"
                         type="text"
                         @click.native.prevent="deleteRow(scope.row.ordNo)">移除</el-button>
            </template></el-table-column>
        </el-table>

        <div v-if="cartTableData.length > 0"
             class="options-box">
          <div class="left">
            <el-checkbox v-model="allChecked"
                         @change="selectAll(allChecked)">全选</el-checkbox>
            <el-button type="text"
                       style="margin-left: 20px">删除所选商品</el-button>
          </div>
          <div class="right">
            <span>已选择{{ allCount }}件商品</span>
            <span>总计:¥{{ totalMoney }}</span>
            <el-button type="danger"
                       style="margin-left: 20px"
                       @click="settleAccount">去结算</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import navHeader from 'components/navHeader.vue'
import { regionData } from 'element-china-area-data'
import { request } from 'network/request'

export default {
  components: { navHeader },
  data() {
    return {
      cartNumber: 0,
      options: regionData,
      region: ['440000', '440100', '440113'],
      cartTableData: [],
      allChecked: false,
      allCount: 0,
      selectedData: [],
      totalMoney: 0,
      checkOrder: [],
    }
  },

  computed: {
    totalPrice() {
      return function (price, count) {
        return price * count
      }
    },
  },

  created() {
    this.getOrder()
  },

  methods: {
    async getOrder() {
      const { count, data } = await request({
        url: '/order',
        params: { page: 1, pageSize: 15, ordSituation: 0 },
      })
      this.cartNumber = count
      this.cartTableData = data
      this.cartTableData.forEach((item) => {
        item.src = require(`assets/image/medical_pics/${item.medName}.jpg`)
      })
    },

    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
    },
    handleRegionChange() {},

    async deleteRow(ordNo) {
      await request({
        url: '/order',
        method: 'delete',
        data: { ordNo },
      })
      this.getOrder()
      this.$store.commit('reduceCount', 1)
    },
    handleSelectionChange(row) {
      this.selectedData = []
      this.allCount = row.length
      let total = 0
      row.forEach(({ ordNo, ordTotalprice }) => {
        this.selectedData.push(ordNo)
        total += ordTotalprice
      })
      this.totalMoney = total.toFixed(2)
    },
    selectAll(allChecked) {
      if (allChecked) {
        this.$refs.cartTable.toggleAllSelection()
      } else {
        this.$refs.cartTable.clearSelection()
      }
    },
    async settleAccount() {
      await request({
        url: '/order',
        method: 'put',
        data: this.selectedData,
      })
      this.getOrder()
      this.$store.commit('reduceCount', this.allCount)
    },
    changeNum() {},
  },
}
</script>

<style lang="less" scoped>
.main {
  width: 1000px;
  margin: 20px auto;
  .cart-filter-bar {
    height: 28px;
    line-height: 28px;
    text-align: center;
    .switch-cart {
      float: left;
      font-size: 16px;
      padding: 0 0 10px;
      cursor: pointer;
      color: #e2231a;
    }
    .cart-store {
      float: right;
      font-size: 14px;
      margin-bottom: 10px;
    }
  }
  .cart-body {
    .el-image {
      vertical-align: middle;
    }
    span {
      padding-left: 20px;
      vertical-align: middle;
    }
    .options-box {
      margin-top: 20px;
      font-size: 12px;
      height: 60px;
      border: 1px solid #eef1f6;
      color: #999;
      .left {
        float: left;
        height: 60px;
        display: flex;
        align-items: center;
      }
      .right {
        float: right;
        height: 60px;
        display: flex;
        align-items: center;
        span:nth-of-type(2) {
          color: red;
          font-size: 16px;
        }
      }
    }
  }
}
</style>